package top.codedance.iotp.serialPortSDK.device.http;

import top.codedance.iotp.serialPortSDK.entity.agy.VrsQll;
import top.codedance.iotp.serialPortSDK.exception.BizException;
import top.codedance.iotp.serialPortSDK.util.SM4Util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * 油气回收-青岛安工院
 * @Description
 * @author ChenPeng
 * @date 2023/5/29 18:41
 */
public class Agy_Vrs implements Vrs {

    private Logger logger = LoggerFactory.getLogger(Agy_Vrs.class);

    private List<VrsQll> vrsInfoList = new ArrayList<>();

    public static Vrs getInstance() {
        return new Agy_Vrs();
    }
    @Override
    public void parse(String result) {
        xmlParse(result);
    }

    private void xmlParse(String xmlStr) {
        Document document = null;
        try {
            document = DocumentHelper.parseText(xmlStr);
        } catch (DocumentException e) {
            throw new BizException("20001", "报文转文档异常！" + System.currentTimeMillis() + "报文:" + xmlStr);
        }
        Element root = document.getRootElement();
        if (("03").equals(root.element("TYPE").getData())) {
            String gasId = root.element("USERID").getData().toString();
            byte[] key = "0123456789abcdef".getBytes();
            String businessContext = root.element("BUSINESSCONTENT").getData().toString();
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] encryptHex = new byte[0];
            try {
                encryptHex = decoder.decode(businessContext);
            } catch (Exception e) {
                throw new BizException("20002-" + System.currentTimeMillis() + "业务报文异常:", businessContext);
            }
            String decryptStr = "";
            if (!("0").equals(root.element("SEC").getData())) {
                try {
                    decryptStr = new String(SM4Util.decrypt_CBC_Padding(key, "905EF8C3C12C3087".getBytes(StandardCharsets.UTF_8), encryptHex));
                } catch (Exception e) {
                    throw new BizException("30001-" + System.currentTimeMillis() + "解密异常:", new String(encryptHex) + e.getMessage());
                }
            } else {
                decryptStr = new String(encryptHex);
            }
            try {
                document = DocumentHelper.parseText(decryptStr);
            } catch (DocumentException e) {
                throw new BizException("20003-" +System.currentTimeMillis() +"解密后报文转文档异常:", decryptStr + ";" + e.getMessage());
            }
            Element rows = document.getRootElement();
            List<Element> rowList = rows.elements("row");
            for (Element row : rowList) {
                if (null != row.element("QLL") && null != row.element("QLL").getData()) {
                    StringBuffer stringBuffer = new StringBuffer();
                    String hDate = row.element("DATE").getData().toString();
                    String qql = row.element("QLL").getData().toString();
                    VrsQll vrsEnt = new VrsQll();
                    vrsEnt.setGasId(gasId);
                    vrsEnt.setHDate(hDate);
                    vrsEnt.setQql(Double.valueOf(qql));
                    vrsInfoList.add(vrsEnt);
                }
            }
        }
    }

    @Override
    public List<VrsQll> getVrsQll() {
        return vrsInfoList;
    }
}
